//
//  FilePathManager.m
//  LPSNews
//
//  Created by Ruan on 14-10-21.
//  Copyright (c) 2014年 LPS. All rights reserved.
//

#import "FilePathManager.h"
#import "NSString+MD5.h"
@implementation FilePathManager


// 创建文件
+ (void)createFilePathString:(NSString *)string
{
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:string]) {
        [manager createDirectoryAtPath:string withIntermediateDirectories:YES attributes:nil error:nil];
    }
}


// 获取文件路径cache文件夹
+ (NSString *)cachePath
{
    NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    return path;
}


// 获取webCachePath文件夹
+ (void)createWebCachePath
{
    NSString *path = [self cachePath];
    NSString *webCachePathString = [path stringByAppendingPathComponent:@"webcache"];
    
    // 判断文件是否存在,不存在则创建
    [self createFilePathString:webCachePathString];

}


// 获取webCachePath文件夹路径
+ (NSString *)webCachePath
{
    NSString *path = [self cachePath];
    NSString *webCachePathString = [path stringByAppendingPathComponent:@"webcache"];
    
    // 判断文件是否存在,不存在则创建
    [self createFilePathString:webCachePathString];
    return webCachePathString;
    
}

+ (NSString *)webCacheFile
{
    NSString *path = [self cachePath];
    NSString *webCachePathString = [path stringByAppendingString:@"webcache"];
    return webCachePathString;
    
}

// 获取图片路径
+ (NSString *)imagePathCache:(NSString *)imageUrl
{
    NSString *path = [self webCachePath];
    path = [path stringByAppendingPathComponent:imageUrl];
    return path;
    
}


// 获取webbody的路径   
+ (NSString *)bodyPathCache:(NSString *)bodyTitle
{
    NSString *path = [self webCachePath];
    NSString *bodyTitleMD5 = [bodyTitle MD5];
    path = [path stringByAppendingPathComponent:bodyTitleMD5];
    return path;
}


// 判断文件是否存在
+ (BOOL)isFileExst:(NSString *)fileName
{
    NSFileManager *fm = [NSFileManager defaultManager];
    if ([fm fileExistsAtPath:fileName]) {
        return YES;
    }else
    {
        return NO;
    }
}


// 清除缓存
+ (void)clearCacheWithPath:(NSString *)string
{
   
    NSFileManager *fm = [NSFileManager defaultManager];
    if ([fm fileExistsAtPath:string]) {
        [fm removeItemAtPath:string error:nil];
    }
}

// 收藏文件夹路径
+ (NSString *)collectorFolderPath
{
    NSString *path =  [self cachePath];
    path = [path stringByAppendingPathComponent:@"collector"];
    [self createFilePathString:path];
    return path;
}

// 返回收藏数据库的路径
+ (NSString *)collectorfilePath
{
    NSString *path = [self collectorFolderPath];
    path = [path stringByAppendingPathComponent:@"db_collector.sqlite"];
    return path;
}


+ (NSString *)collectorBodyPathCache:(NSString *)bodyTitle
{
    NSString *path = [self collectorFolderPath];
    NSString *bodyTitleMD5 = [bodyTitle MD5];
    path = [path stringByAppendingPathComponent:bodyTitleMD5];
    return path;
}


// 创建收藏数据库

+ (void)createCollectorfile
{
    NSString *path = [self collectorFolderPath];
    path = [path stringByAppendingPathComponent:@"db_collector.sqlite"];
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:path]) {
        [manager createFileAtPath:path contents:nil attributes:nil];
    }
}


// 删除文件

+ (void)deleteCollectFileWithPath:(NSString *)string
{
    NSFileManager *fm = [NSFileManager defaultManager];
    NSString *path = [[self collectorFolderPath] stringByAppendingPathComponent:string];
    if ([fm fileExistsAtPath:path]) {
       
        [fm removeItemAtPath:path error:nil];
    }
  
}


@end
